/**
 * Created by Xiaoming on 2017/8/29.
 */
angular.module('module.auth')
    .controller('LoginController', LoginController);

LoginController.$inject = [ '$window', '$stateParams', 'AuthService', 'AuthUserService', 'WxAuthService', 'UserinfoService'];
function LoginController($window, $stateParams, AuthService, AuthUserService, WxAuthService, UserinfoService) {
    var vm = this;
    vm.user = {};
    vm.backurl = decodeURIComponent($stateParams.backurl);
    vm.login = login;
    vm.goBack = goBack;
    vm.wxlogin = wxlogin;
    activate();
    function activate(){
        // 尝试微信登录
        WxAuthService.wxTryLogin(UserinfoService.getUserinfoByOpenid, function (userinfo) {
            //alert("WxAuthService.wxTryLogin() userinfo="+JSON.stringify(userinfo));
            vm.user.nickname = userinfo.nickname;
            vm.user.logo = userinfo.logo;
            AuthUserService.addUserToCookies(userinfo);
            setTimeout(function () {
                $window.history.back();
            }, 500);
        }, function (error, openid) {
            //alert(JSON.stringify(error));
            //alert(openid);
        });
    }
    // 用户名密码登录
    function login() {
        if (!vm.user.name) {
            input_alert("请输入用户名", 2000);
            return;
        }
        if (!vm.user.password) {
            input_alert("请输入密码", 2000);
            return;
        }
        AuthService.userlogin(vm.user, function (data) {
            AuthUserService.addUserToCookies(data);
            setTimeout(function () {
                window.location.href = vm.backurl;
            }, 500);
        }, function (reason) {
            if (reason.data.reason) {
                input_alert(reason.data.reason);
            } else {
                input_alert(reason.data.message);
            }
        });
    }
    //回退操作
    function goBack() {
        setCookie("_login", "back");
        window.location.href = vm.backurl;
    }
    // 尝试微信登录或跳转注册
    function wxlogin() {
        var redirect_uri = window.location.href.replace("/login", "/wxuc");
        function success_cbfunc(userinfo) {
            AuthUserService.addUserToCookies(userinfo);
            setTimeout(function () {
                window.location.href = vm.backurl;
            }, 500);
        }

        function error_cbfunc(error, openid) {
        }

        //
        //alert("WxAuthService.wxTryLoginOrRegist()");
        WxAuthService.wxTryLoginOrRegist(redirect_uri, UserinfoService.getUserinfoByOpenid, success_cbfunc, error_cbfunc);
    }
    //用户appid获取入库操作
    //var param = {
    //    "component_appid":"wxae27378e816cd807",
    //    "mer_id": "wx3fc50228690a1289",
    //    "next_openid": ""
    //};
    //vm.unified = function(){
    //    console.log(param);
    //    AuthService.getUserByAppId(param,function(data){
    //        var openidlist = JSON.parse(data.data);
    //        console.log(openidlist);
    //        userGet(openidlist.openid,0);
    //    },function(){
    //
    //    })
    //};
    //function userGet(openidlist,i){
    //    if(i>=openidlist.length){
    //        return;
    //    }
    //    var item = openidlist[i];
    //    console.log(item);
    //    AuthService.getByOpenid(item,function(user){
    //        console.log(user);
    //        user.password = param.mer_id;
    //        AuthService.update(user,function(){
    //            console.log("用户更新成功");
    //        },function(){
    //            console.log("用户更新失败");
    //        });
    //        userGet(openidlist,i+1);
    //    },function(error){
    //        userGet(openidlist,i+1);
    //        console.log(error);
    //        console.log("获取用户失败");
    //    })
    //}
    //vm.unified();
    //比对外卖用户
}